from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys
import json
from tools.MyException import DataError
from tools.BaseInfo import BaseInfo
import PyQt5.QtCore
from tools.functiontools import is_float
from tools.MyWidget import AttachmentWidget

class ClassManager(QWidget):
    def __init__(self) -> None:
        super().__init__()
        self.setGeometry(350,250,770,500)
        self.layout = QVBoxLayout()
        self.setLayout(self.layout)
        self.main_widget = QWidget()
        self.main_layout = QHBoxLayout()
        self.main_widget.setLayout(self.main_layout)
        self.left = LeftWidget()
        self.main_layout.addWidget(self.left)
        self.right = [BaseInfo(),FileWidget(),RightWidget()]
        self.main_layout.addWidget(self.right[0])
        self.top = TopWidget()
        self.layout.addWidget(self.top)
        self.layout.addWidget(self.main_widget)
        self.left.get_widget_pointer(self.right,self.main_layout)
        self.left.get_label_pointer(self.top.tittle)
class TopWidget(QWidget):
    def __init__(self) -> None:
        super().__init__()
        pal = QPalette(self.palette())
        pal.setColor(QPalette.Background,QColor(255,255,255))
        self.setAutoFillBackground(True)
        self.setPalette(pal)
        self.layout = QHBoxLayout()
        self.layout.setContentsMargins(0,0,0,0)
        self.setLayout(self.layout)
        #设置logo
        self.left_pic_widget = QWidget()
        self.logo = QLabel(self.left_pic_widget)
        self.pix = QPixmap("static/cqu.jpg")
        self.pix = self.pix.scaled(39, 39, Qt.KeepAspectRatio)
        self.logo.setPixmap(QPixmap(self.pix))
        #设置标题
        self.tittle = QLabel(self.left_pic_widget)
        self.tittle.setText("新建课程")
        font = QFont('PingFang SC Regular',20)
        font.setPixelSize(20)
        self.tittle.setFont(font)
        self.layout.addWidget(self.left_pic_widget)
        self.tittle.move(55,0)
        self.side = QLabel(self)
        self.side.setFixedSize(60,35)
        self.pic = QPixmap("static/people.jpg")
        self.pic = self.pic.scaled(42, 33, Qt.KeepAspectRatio)
        self.side.setPixmap(QPixmap(self.pic))
        self.layout.addWidget(self.side)
        self.setFixedHeight(57)
class LeftWidget(QWidget):
    def __init__(self) -> None:
        super().__init__()
        self.btn1 = QPushButton("基本信息",self)
        self.btn2 = QPushButton("详细目标",self)
        self.btn3 = QPushButton("相关文件",self)
        self.setFixedWidth(134)
        self.init_ui()
    def init_ui(self):
        self.set_widget_style()
        pal = QPalette(self.palette())
        pal.setColor(QPalette.Background,QColor(255,255,255))
        self.setAutoFillBackground(True)
        self.setPalette(pal)
        self.btn1.clicked.connect(self.base_message)
        self.btn2.clicked.connect(self.score_content)
        self.btn3.clicked.connect(self.releted_file)
        self.btn1.move(15,30)
        self.btn2.move(15,130)
        self.btn3.move(15,230)
    def set_widget_style(self):
        font = QFont('PingFang SC Regular')
        font.setPixelSize(20)
        self.btn1.setStyleSheet("color:rgb(73,166,234);border:none;")
        self.btn2.setStyleSheet("color:rgb(170,170,170);border:none;")
        self.btn3.setStyleSheet("color:rgb(170,170,170);border:none;")
        self.btn1.setFont(font)
        self.btn2.setFont(font)
        self.btn3.setFont(font)
    def get_widget_pointer(self,widget,layout):
        self.right = widget
        self.right_layout = layout
    def get_label_pointer(self,label):
        self.title_lab = label
    def score_content(self):
        for i in range(len(self.right)):
            self.right[i].close()
        self.right[2].show()
        self.right_layout.addWidget(self.right[2])
        self.title_lab.setText("分值填写")
        self.btn1.setStyleSheet("color:rgb(170,170,170);border:none;")
        self.btn2.setStyleSheet("color:rgb(73,166,234);border:none;")
        self.btn3.setStyleSheet("color:rgb(170,170,170);border:none;")
    def base_message(self):
        for i in range(len(self.right)):
            self.right[i].close()
        self.right[0].show()
        self.right_layout.addWidget(self.right[0])
        self.title_lab.setText("新建课程")
        self.btn1.setStyleSheet("color:rgb(73,166,234);border:none;")
        self.btn2.setStyleSheet("color:rgb(170,170,170);border:none;")
        self.btn3.setStyleSheet("color:rgb(170,170,170);border:none;")
    def releted_file(self):
        for i in range(len(self.right)):
            self.right[i].close()
        self.right[1].show()
        self.right_layout.addWidget(self.right[1])
        self.title_lab.setText("新建课程")
        self.btn1.setStyleSheet("color:rgb(170,170,170);border:none;")
        self.btn2.setStyleSheet("color:rgb(170,170,170);border:none;")
        self.btn3.setStyleSheet("color:rgb(73,166,234);border:none;")
class FileWidget(QWidget):
    def __init__(self) -> None:
        super().__init__()
        self.resize(590,390)
        self.layout = QHBoxLayout()
        self.setLayout(self.layout)
        self.left = AttachmentWidget(path="static/file.jpg",title="附件清单")
        self.right = AttachmentWidget(path="static/add.jpg",title="提交支撑材料")
        self.layout.addWidget(self.left)
        self.layout.addWidget(self.right)
    """        
    def init_ui(self):
        pal = QPalette(self.palette())
        pal.setColor(QPalette.Background,QColor(242,242,242))
        self.setAutoFillBackground(True)
        self.setPalette(pal)
        self.wid1.pal = QPalette(self.wid1.palette())
        self.wid1.pal.setColor(QPalette.Background,QColor(255,255,255))
        self.wid1.setAutoFillBackground(True)
        self.wid1.setPalette(self.wid1.pal)
        self.wid2.pal = QPalette(self.wid2.palette())
        self.wid2.pal.setColor(QPalette.Background,QColor(255,255,255))
        self.wid2.setAutoFillBackground(True)
        self.wid2.setPalette(self.wid2.pal)
        self.lab1.move(5,5)
        self.lab2.move(50,15)
        self.lab4.move(300,5)
        self.lab3.move(340,15)
        self.btn1.move(0,335)
        self.btn2.move(155,335)
        self.btn3.move(295,335)
        self.btn4.move(445,335)
        self.wid1.move(5,45)
        self.wid2.move(300,45)
    def set_widget_style(self):
        f = QFont("PingFang SC")
        f.setPixelSize(18)
        self.lab3.setFont(f)
        self.lab2.setFont(f)
        self.btn1.setFixedWidth(140)
        self.btn2.setFixedWidth(140)
        self.btn3.setFixedWidth(140)
        self.btn4.setFixedWidth(140)
        self.wid1.setFixedSize(283,288)
        self.wid2.setFixedSize(283,288)
    """
class RightWidget(QWidget):
    def __init__(self) -> None:
        super().__init__()
        self.resize(590,390)
        self.btn1 = QPushButton("添加细则",self)
        self.btn2 = QPushButton("删除环节",self)
        self.btn3 = QPushButton("增加环节",self)
        self.btn4 = QPushButton("增加顶层环节",self)
        self.btn5 = QPushButton("导出",self)
        self.btn6 = QPushButton("确定",self)
        #课程目标表
        self.lab = QLabel(self)
        self.lab.move(5,5)
        self.pic = QPixmap("static/board.jpg")
        self.pic = self.pic.scaled(37, 37, Qt.KeepAspectRatio)
        self.lab.setPixmap(self.pic)
        self.tittle = QLabel(self)
        self.tittle.setText("课程目标表")
        font = QFont('PingFang SC Regular',20)
        font.setPixelSize(20)
        self.tittle.setFont(font)

        #环节  分值  比例  目标1  目标2  目标3 目标4
        self.tree = QTreeWidget(self)
        self.tree.setColumnCount(7)
        self.tree.header().setVisible(False)
        root = QTreeWidgetItem(self.tree)
        root.setText(0,"环节")
        root.setText(1,"分值")
        root.setText(2,"比例")
        root.setText(3,"目标1")
        root.setText(4,"目标2")
        root.setText(5,"目标3")
        root.setText(6,"目标4")
        self.goal_list = []
        self.tree_list = []

        self.init_ui()
        self.set_widget_style()
    def init_ui(self):
        pal = QPalette(self.palette())
        pal.setColor(QPalette.Background,QColor(255,255,255))
        self.setAutoFillBackground(True)
        self.setPalette(pal)
        self.btn2.clicked.connect(self.del_goal)
        self.btn3.clicked.connect(self.add_goal)
        self.btn4.clicked.connect(self.add_top_goal)
        self.btn5.clicked.connect(self.export_goal)
        self.btn1.move(480,10)
        self.btn2.move(385,10)
        self.btn3.move(290,10)
        self.btn4.move(170,10)
        self.tittle.move(50,10)
        self.btn5.move(525,335)
        self.btn6.move(475,335)
        self.tree.move(10,45)
    def add_goal(self):
        item = self.tree.currentItem()
        child = QTreeWidgetItem(item)
        child.setText(0,"子环节")
        child.setFlags(child.flags()|PyQt5.QtCore.Qt.ItemIsEditable)
    def add_top_goal(self):
        child = QTreeWidgetItem(self.tree)
        child.setText(0,"新环节")
        child.setFlags(child.flags()|PyQt5.QtCore.Qt.ItemIsEditable)
        self.goal_list.append(child)
    def del_goal(self):
        rootFather=self.tree.invisibleRootItem()#获得根节点root的不可见的父节点
        for item in self.tree.selectedItems():
            (item.parent() or rootFather).removeChild(item)
    def export_goal(self):
        self.usual_grade = []
        self.usual_dict = []
        for n in range(1,self.tree.topLevelItemCount()):
            if self.tree.topLevelItem(n).childCount():
                self.usual_dict.append([])
                for i in range(self.tree.topLevelItem(n).childCount()):
                    self.usual_dict[n-1].append(self.tree.topLevelItem(n).text(0))
                    self.usual_dict[n-1].append(self.tree.topLevelItem(n).text(1))
                    self.usual_dict[n-1].append(self.tree.topLevelItem(n).text(2))
                    self.usual_dict[n-1].append(self.get_message(self.tree.topLevelItem(n).child(i)))#.child(i)
        ##将树状结构存入json
        filename = 'tree.json'
        with open(filename, 'w') as file_obj:
            json.dump(self.usual_dict,file_obj)
        

    def get_message(self,tree):
        result = []
        result.append(tree.text(0))
        result.append(tree.text(1))
        result.append(tree.text(2))
        if (not is_float(tree.text(1))) or  (not is_float(tree.text(2))):
            raise DataError("分值和比例必须填写且为数字")
        for i in range(tree.childCount()):
            if tree.child(i).childCount():
                result.append(tree.child(i).text(0))
                try:
                    result.extend(self.get_message(tree.child(i)))
                except:
                    raise
            else:
                count = tree.child(i).columnCount()
                tmp = []
                for j in range(count):
                    tmp.append(tree.child(i).text(j))
                result.append(tmp)
        return result
        
    def set_widget_style(self):
        tfont = QFont("PingFang SC Regular")
        tfont.setPixelSize(15)
        self.btn1.setFont(tfont)
        self.btn2.setFont(tfont)
        self.btn3.setFont(tfont)
        self.btn4.setFont(tfont)
        self.btn5.setFont(tfont)
        self.btn6.setFont(tfont)
        self.btn1.setStyleSheet("border-radius: 5px;border: 1px groove rgb(126, 126, 126); ")
        self.btn2.setStyleSheet("border-radius: 5px;border: 1px groove rgb(126, 126, 126);")
        self.btn3.setStyleSheet("border-radius: 5px;border: 1px groove rgb(126, 126, 126);")
        self.btn4.setStyleSheet("border-radius: 5px;border: 1px groove rgb(126, 126, 126);")
        self.btn5.setStyleSheet("border-radius: 5px;border: 1px groove rgb(126, 126, 126);")
        self.btn6.setStyleSheet("border-radius: 5px;border: 1px groove rgb(126, 126, 126);")
        self.tree.setFixedSize(550,280)
if __name__=="__main__":
    app = QApplication(sys.argv)
    window = ClassManager()
    window.show()
    sys.exit(app.exec_())